package cn.zzz.gen;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author micheal 常用数据库类型
 * <p>
 * char(?) varchar(?) text
 * <p>
 * date datetime timestamp
 * <p>
 * int(?) tinyint(?) decimal(?,?) bingint(？)
 * double
 */
public class Column {
    private String Field;
    private String Type;
    private String Null;
    private String Key;
    private String Default;
    private String Extra;

    public String dbType() {
        int index = Type.indexOf("(");
        String type = (index == -1 ? Type : Type.substring(0, index));
        return type.toLowerCase();
    }



    public String jdbcType() {
        String dbType = dbType();
        String jdbcType = jdbcTypeMap.get(dbType);
        if (jdbcType == null) {
            throw new RuntimeException("找不到对应jdbc类型：" + dbType);
        }
        return jdbcType;
    }



    public Class<?> javaType() {
        String dbType = dbType();
        Class<?> clazz = javaTypeMap.get(dbType);
        if (clazz == null) {
            throw new RuntimeException("找不到对应java类型：" + dbType);
        }
        return clazz;
    }



    public Boolean needImport() {
        return !javaType().getPackage().getName().startsWith("java.lang");
    }

    public Boolean isPK() {
        return "PRI".equals(this.Key);
    }

    public Boolean isFK() {
        return isPK() ? false : this.Field.endsWith("Id") && (this.Type.startsWith("int") || this.Type.startsWith("bigint"));
    }

    public Boolean isCreateTime() {
        return this.Field.endsWith("reateTime") && this.Type.startsWith("timestamp");
    }

    public Boolean isVersion() {
        return this.Field.equalsIgnoreCase("version");
    }

    private static Map<String, Class<?>> javaTypeMap = new HashMap<>();

    static {
        javaTypeMap.put("char", String.class);
        javaTypeMap.put("varchar", String.class);
        javaTypeMap.put("text", String.class);
        javaTypeMap.put("tinytext", String.class);
        javaTypeMap.put("date", Date.class);
        javaTypeMap.put("datetime", Date.class);
        javaTypeMap.put("timestamp", Date.class);
        javaTypeMap.put("tinyint", Byte.class);
        javaTypeMap.put("smallint", Short.class);
        javaTypeMap.put("int", Integer.class);
        javaTypeMap.put("bigint", Long.class);
        javaTypeMap.put("double", Double.class);
        javaTypeMap.put("float", Float.class);
        javaTypeMap.put("decimal", BigDecimal.class);

    }

    private static Map<String, String> jdbcTypeMap = new HashMap<>();

    static {
        jdbcTypeMap.put("char", "CHAR");
        jdbcTypeMap.put("varchar", "VARCHAR");
        jdbcTypeMap.put("text", "VARCHAR");
        jdbcTypeMap.put("tinytext", "VARCHAR");
        jdbcTypeMap.put("date", "DATE");
        jdbcTypeMap.put("datetime", "TIMESTAMP");
        jdbcTypeMap.put("timestamp", "TIMESTAMP");
        jdbcTypeMap.put("tinyint", "TINYINT");
        jdbcTypeMap.put("smallint", "SMALLINT");
        jdbcTypeMap.put("int", "INTEGER");
        jdbcTypeMap.put("bigint", "LONG");
        jdbcTypeMap.put("double", "DOUBLE");
        jdbcTypeMap.put("float", "FLOAT");
        jdbcTypeMap.put("decimal", "DECIMAL");
    }

    //
    public String getField() {
        return Field;
    }

    public void setField(String field) {
        Field = field;
    }

    public String getType() {
        return Type;
    }

    public void setType(String type) {
        Type = type;
    }

    public String getNull() {
        return Null;
    }

    public void setNull(String null1) {
        Null = null1;
    }

    public String getKey() {
        return Key;
    }

    public void setKey(String key) {
        Key = key;
    }

    public String getDefault() {
        return Default;
    }

    public void setDefault(String default1) {
        Default = default1;
    }

    public String getExtra() {
        return Extra;
    }

    public void setExtra(String extra) {
        Extra = extra;
    }

    public String genGetter() {
        return CamelUtils.genGetter(this.Field);
    }

    public String genFKObjGetter() {
        return CamelUtils.genGetter(genFKObj());

    }

    public String genFKObj() {
        return this.Field.substring(0, this.Field.length() - 2);

    }

    public String genFKObjType() {
        return CamelUtils.firstChar2Upper(genFKObj());

    }
}
